import {Component, OnInit} from '@angular/core';
import {CommonService} from '../../common.service';

@Component({
    selector: 'app-register',
    templateUrl: './register.component.html',
    styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
    regInfo = {
        key: '',
        verify: '',
        username: '',
        // mailcode: '',
        nickname: '',
        password: '',
        repassword: ''
    };
    verify;
    key;

    constructor(private Service: CommonService) {
    }

    ngOnInit() {
        this.getCode();
    }

    register() {
        const $this = this;
        let index = layer.load();
        this.Service.post('login/register', this.regInfo).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                layer.close(index);
                if (res['status'] == 1) {
                    layer.msg('注册成功');
                    $this.Service.setItem('user', JSON.stringify(data));
                    setTimeout(function () {
                        location.href = '/';
                    }, 2000);
                } else {
                    // 提示
                    layer.msg(msg);
                }
                $this.getCode();
            });
        });
    }

    /* 获取验证码 */
    getCode() {
        const $this = this;
        this.Service.post('login/verify', {}).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.verify = data.img;
                $this.key = data.key;
                $this.regInfo.key = data.key;
            });
        });
    }

    send_email() {
        const params = {
            username: this.regInfo.username,
            type: 'send_email'
        };

        this.Service.post('login/register', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {

            });
        });
    }
}
